using Uantek.Device.Communication.Models;

namespace Uantek.Device.Communication.Services.Interfaces
{
    /// <summary>
    /// MQTT发布服务接口
    /// </summary>
    public interface IMqttPublishService
    {
        /// <summary>
        /// MQTT连接状态
        /// </summary>
        bool IsConnected { get; }

        /// <summary>
        /// 连接到MQTT服务器
        /// </summary>
        /// <param name="serverAddress">服务器地址</param>
        /// <param name="port">端口号</param>
        /// <param name="clientId">客户端ID</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>连接结果</returns>
        Task<CommunicationResult> ConnectAsync(string serverAddress, int port, string clientId, string? username = null, string? password = null);

        /// <summary>
        /// 断开MQTT连接
        /// </summary>
        /// <returns>断开结果</returns>
        Task<CommunicationResult> DisconnectAsync();

        /// <summary>
        /// 发布设备数据
        /// </summary>
        /// <param name="deviceData">设备数据</param>
        /// <param name="topic">主题（可选，如果为空则使用默认主题）</param>
        /// <returns>发布结果</returns>
        Task<CommunicationResult> PublishDeviceDataAsync(DeviceDataModel deviceData, string? topic = null);

        /// <summary>
        /// 发布设备状态
        /// </summary>
        /// <param name="deviceStatus">设备状态</param>
        /// <param name="topic">主题（可选，如果为空则使用默认主题）</param>
        /// <returns>发布结果</returns>
        Task<CommunicationResult> PublishDeviceStatusAsync(DeviceStatus deviceStatus, string? topic = null);

        /// <summary>
        /// 发布自定义消息
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="payload">消息内容</param>
        /// <param name="retain">是否保留消息</param>
        /// <returns>发布结果</returns>
        Task<CommunicationResult> PublishMessageAsync(string topic, string payload, bool retain = false);

        /// <summary>
        /// 批量发布设备数据
        /// </summary>
        /// <param name="deviceDataList">设备数据列表</param>
        /// <returns>发布结果</returns>
        Task<CommunicationResult> PublishBatchDeviceDataAsync(IEnumerable<DeviceDataModel> deviceDataList);

        /// <summary>
        /// 设置默认主题前缀
        /// </summary>
        /// <param name="topicPrefix">主题前缀</param>
        void SetDefaultTopicPrefix(string topicPrefix);

        /// <summary>
        /// 获取默认主题前缀
        /// </summary>
        /// <returns>主题前缀</returns>
        string GetDefaultTopicPrefix();

        /// <summary>
        /// MQTT连接状态变化事件
        /// </summary>
        event EventHandler<bool>? ConnectionStatusChanged;

        /// <summary>
        /// 消息发布完成事件
        /// </summary>
        event EventHandler<(string Topic, bool Success)>? MessagePublished;
    }
}